/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,$stateParams,$localStorage,RequestService,commonConfig,$modalInstance,parentKey,piecesList) {
    console.log(parentKey,piecesList);
    $scope.User =$localStorage.get('cloud');
    RequestService.Request(commonConfig.cloudApi+'cloud/html/getList?userId='+ $scope.User.detail.id+'&isModule=true&isChart=false&tenantId='+$scope.User.detail.tenantId,'GET').then(function(res){
        console.log(res);
        $scope.tabList = res;
    },function(error){
        console.log(error);
    });
    //选中数组
    $scope.checkedArry = [];
    //全选功能
    $scope.checkAll = function () {
        if ($scope.check_all) {
            $scope.checkedArry = [];
            angular.forEach($scope.tabList, function (i) {
                i.checked = true;
                $scope.checkedArry.push(i.id);
            });
        } else {
            angular.forEach($scope.tabList, function (i) {
                i.checked = false;
                $scope.checkedArry = [];
            });
        }
    };
    //单选功能
    $scope.checkOne = function () {
        angular.forEach($scope.tabList, function (i) {
            var index = $scope.checkedArry.indexOf(i.id);
            if (i.checked && index === -1) {
                $scope.checkedArry.push(i.id);
            } else if (!i.checked && index !== -1) {
                $scope.checkedArry.splice(index, 1);
            }
        });
        $scope.check_all = ($scope.tabList.length === $scope.checkedArry.length) ? true : false;
    };
    //提交
    $scope.ok = function () {
        //添加tab标签数组
        piecesList[parentKey].childIds = piecesList[parentKey].childIds.concat($scope.checkedArry);
        var arr = [];
        //生成框架结构
        angular.forEach(piecesList,function(val,i){
            arr.push({ "childIds": val.childIds, "id": val.id});
        });
        //清空选择数组
        $scope.checkedArry = [];
        $modalInstance.close(arr);
    };
    //关闭modal
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }
};